package main

import (
	"context"
	"github.com/gin-gonic/gin"
	"go-admin-official/modules/config"
	"go-admin-official/modules/logger"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func startServer(router *gin.Engine) {
	srv := &http.Server{
		Addr:    ":" + config.Get().Port,
		Handler: router,
	}

	go func() {
		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			logger.Fatal("listen: %s\n", err)
		}
	}()

	quit := make(chan os.Signal)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit
	logger.Info("Shutting down server...")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	if err := srv.Shutdown(ctx); err != nil {
		logger.Fatal("Server forced to shutdown:", err)
	}
	logger.Info("Server exiting")
}
